/*
Summation of primes
Problem 10

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.
*/

var isPrime = function (n) {
    if (isNaN(n) || !isFinite(n) || n % 1 || n < 2) {
        return false;
    };

    if (n % 2 == 0) {
        return (n == 2);
    }

    var m = Math.sqrt(n);

    for (var i = 3; i <= m; i += 2) {
        if (n % i == 0) {
            return false;
        }
    }

    return true;
};

var solveTask = function () {
    var sum = 0;

    for (var l = 0; l < 2000000; l++) {
        if (isPrime(l))
            sum += l;
    }

    return sum;
};

var linq = require('linq');
var start = new Date().getTime();

var result = solveTask();

console.log("Result: ", result);

var end = new Date().getTime();
var time = end - start;

console.log("Time: ", time);

require('readline')
    .createInterface(process.stdin, process.stdout)
    .question("Press [Enter] to exit...", function(){
        process.exit();
});